/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 *
 * @Original Author: jin Chen
 * @date: Nov 17, 2003$
 * @version: 1.0
 */
package edu.psu.geovista.app.dataloader.v1;

//import edu.psu.geovista.data.model.AppDataModel;
//import edu.psu.geovista.ui.dataloader.event.TSDataEvent;
import edu.psu.geovista.common.awt.event.dataloading.TSDataListener;
import edu.psu.geovista.ui.event.DataSetListener;

import javax.swing.*;
import java.awt.*;
import java.beans.BeanDescriptor;
import java.beans.BeanInfo;
import java.beans.MethodDescriptor;
import java.beans.SimpleBeanInfo;
import java.lang.reflect.Method;
import java.net.URL;

public class DataLoadersBeanInfo extends SimpleBeanInfo {
    public static final boolean DEBUG=false;
    final public static String RESOURCES="resources/";
    public static String MODEL_ROOT= "edu/psu/geovista/app/pcp/dataloader/";//edu/psu/geovista/app/pcp/";
    public static String IMAGES=MODEL_ROOT+RESOURCES+"images/";

    private static Object[] methods
            ={"addDataSetListener",new Class[]{DataSetListener.class},
              "addTsDataListener" ,new Class[]{TSDataListener.class },
              "removeDataSetListener",new Class[]{DataSetListener.class },
               "removeTsDataListener",new Class[]{TSDataListener.class }
               };
   static{
         String bp=DataLoaders.class.getPackage().getName() ;
         String bpkg=bp.replace('.','/');
         bpkg=bpkg+'/';
         MODEL_ROOT=bpkg;
         IMAGES=MODEL_ROOT+RESOURCES+"images/";
         if(DEBUG)
         System.out.println("!  ");
    }

    //private static String[] methodNames={};

    private final static Class beanClass=DataLoaders.class;
    public BeanDescriptor getBeanDescriptor() {
        return new BeanDescriptor(beanClass);
    }
    public MethodDescriptor[] getMethodDescriptors() {
            /* Method  setData1,setData2,setData3,
                    setSelectedIndex,dataSetChanged,selectionChanged,
                    addColorL,addDataSetL,addIndicateL,addSelectL,addTsL;

            try {
                Class[] args={Object[].class};
                setData1=PcpTimeSeries.class.getMethod("setData",args);

                Class[] args4={int[].class };
                setSelectedIndex=PcpTimeSeries.class.getMethod("setSelectedIndex",args4);

                Class[] args5={DataSetEvent.class };
                dataSetChanged=PcpTimeSeries.class.getMethod("dataSetChanged",args5);

                Class[] args6={SelectionEvent.class };
                selectionChanged=PcpTimeSeries.class.getMethod("selectionChanged",args6);


                MethodDescriptor[] result={
                    new MethodDescriptor(setData1),
                    new MethodDescriptor(setSelectedIndex),
                    new MethodDescriptor(dataSetChanged),
                    new MethodDescriptor(selectionChanged)
               };
                return result;
            } catch (NoSuchMethodException e) {
                e.printStackTrace() ;
                return null;

            } catch (SecurityException e) {
                e.printStackTrace() ;
            }
            return null;*/
           return this.generateMD() ;


        }

        private MethodDescriptor[] generateMD(){
             MethodDescriptor[] result=new MethodDescriptor[methods.length /2];
                 for (int i=0;i<methods.length ;i=i+2){
                     String methodName=(String) methods[i];
                     Class[] argument=(Class[]) methods[i+1];
                     try {
                         Method m=DataLoaders.class.getMethod(methodName,argument); // 
                         result[i/2]=new MethodDescriptor(m);
                     } catch (NoSuchMethodException e) {

                         e.printStackTrace();  //To change body of catch statement use Options | File Templates.
                         continue;
                     } catch (SecurityException e) {
                         e.printStackTrace();  //To change body of catch statement use Options | File Templates.
                         continue;
                     }
                 }
            return result;
        }

        public Image getIcon(int iconKind){
            if (iconKind == BeanInfo.ICON_MONO_16x16 ||
                iconKind == BeanInfo.ICON_COLOR_16x16 )
            {

              java.awt.Image img = getImage(IMAGES+"DataLoader16.gif");  // 
                if(DEBUG){
                    System.out.println("load pcp image:"+img);
                }
              return img;
            }
            if (iconKind == BeanInfo.ICON_MONO_32x32 ||
                iconKind == BeanInfo.ICON_COLOR_32x32 )
            {
              //if(Debug.isDebug()) {
                  String im = IMAGES + "DataLoader32.gif";
                   if(DEBUG){
                    System.out.println(this.getClass().getName() +" try load  image:"+im);
                }
              //}
              //java.awt.Image img = getImage(IMAGES +"PcpTs32.gif");
              java.awt.Image img = getImage(im);
              if(DEBUG){
                    System.out.println("loaded pcp image:"+img);
              }
              return img;
            }
            return null;

        }  //getIcon
        // loading images
        public static Image getImage(String fullName) {
            //String fullName=IMAGES+name;
            //todo change the name of class to current BeanInfo class
            ClassLoader cl=DataLoadersBeanInfo.class.getClassLoader() ; // 
            URL url=cl.getResource(fullName);
            if (url == null) {
                System.out.println("image "+fullName+" not found");
                return null;
            }
            return new ImageIcon(url).getImage() ;
        }


}
